import http from '@/utils/request'
import { removeTokenInfo, setTokenInfo } from '@/utils/storage'
// 获取验证码请求
export const sendCode = (mobile) => {
  return () => {
    http.get(`/sms/codes/${mobile}`)
  }
}

// token
export const saveToken = (payload) => {
  return {
    type: 'login-token',
    payload,
  }
}

// 登录请求
export const login = (data) => {
  return async (dispatch) => {
    const res = await http.post(`/authorizations`, data)
    dispatch(saveToken(res.data))
    setTokenInfo(res.data)
  }
}

// 退出登录
export const onlogout = () => {
  return (dispatch) => {
    removeTokenInfo()
    dispatch({
      type: 'login/logout',
    })
  }
}
